package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.dto.DictDTO;
import com.xfcy.blog.entity.DictData;
import com.xfcy.blog.mapper.DictMapper;
import com.xfcy.blog.entity.Dict;
import com.xfcy.blog.service.DictDataService;
import com.xfcy.blog.service.DictService;
import com.xfcy.blog.utils.BeanCopyUtil;
import com.xfcy.blog.utils.HumpLineUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 字典表(Dict)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@Service("dictService")
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Resource
    private DictDataService dictDataService;

    /**
     * 字典列表
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param isPublish
     * @param ascColumn
     * @param desColumn
     * @return
     */
    @Override
    public R listDict(Integer pageNo, Integer pageSize, String name, Integer isPublish, String ascColumn, String desColumn) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<Dict>()
                .eq(isPublish != null, SqlConstant.IS_PUBLISH, isPublish)
                .like(StringUtils.isNotBlank(name), SqlConstant.NAME, name);

        if (StringUtils.isNotEmpty(ascColumn)) {
            String column = HumpLineUtils.humpToLine(ascColumn);
            queryWrapper.orderByAsc(StringUtils.isNotEmpty(column), column);
        } else if (StringUtils.isNotEmpty(desColumn)) {
            String column = HumpLineUtils.humpToLine(desColumn);
            queryWrapper.orderByAsc(StringUtils.isNotEmpty(column), column);
        } else {
            queryWrapper.orderByDesc(SqlConstant.SORT);
        }

        Page<Dict> data = baseMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
        return R.success(data);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchDict(List<Long> ids) {
        int count = dictDataService.count(new QueryWrapper<DictData>().in(SqlConstant.DICT_TYPE_ID, ids));
        Assert.isTrue(count==0,"所选字典类型中存在字典数据!");
        baseMapper.deleteBatchIds(ids);
        return R.success();
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteDict(Long id) {
        int count = dictDataService.count(new QueryWrapper<DictData>().eq(SqlConstant.DICT_TYPE_ID, id));
        Assert.isTrue(count == 0, "该字典类型存在字典数据");
        baseMapper.deleteById(id);
        return R.success();
    }

    /**
     * 添加字典
     * @param dictDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addDict(DictDTO dictDTO) {
        validateType(dictDTO.getType());
        Dict dict = BeanCopyUtil.copyObject(dictDTO, Dict.class);
        baseMapper.insert(dict);
        return R.success();
    }

    /**
     * 修改字典
     * @param dictDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDict(DictDTO dictDTO) {
        Dict dict = BeanCopyUtil.copyObject(dictDTO, Dict.class);
        Dict temp = baseMapper.selectById(dict.getId());
        if (!temp.getType().equals(dict.getType())) {
            validateType(dict.getType());
        }
        baseMapper.updateById(dict);
        return R.success();
    }

    /* ---------自定义方法开始------------*/
    public void validateType(String type){
        Dict temp  = baseMapper.selectOne(new QueryWrapper<Dict>().eq(SqlConstant.TYPE, type).last(SqlConstant.LIMIT_ONE));
        Assert.isNull(temp,"该字典类型已存在!");
    }
}


